import * as Cesium from 'cesium/Cesium'
import MeasurementSettings from "./MeasurementSettings";
import PolylinePrimitive from "./PolylinePrimitive";
import MeasureUnits from "./MeasureUnits";
import Measurement from "./Measurement";
import getWorldPosition from "./getWorldPosition";

var scratch = new Cesium.Cartesian3
    , scratchCarto$3 = new Cesium.Cartographic;
function getIcon(e) {
    return '<svg viewBox="0 0 30 30" height="' + e + 'px" width="' + e + 'px">\n             <g transform="translate(0,-267)">\n               <path d="m 15.042838,272.34414 0.01712,19.60575"/>\n               <circle r="2.0788691" cy="270.01154" cx="15.078616"/>\n               <path d="m 0.64901081,296.20687 8.80039389,-6.01044 7.9375003,3.1183 12.347278,-3.34365"/>\n             </g>\n           </svg>'
}
function HeightMeasurement(e) {
    e = Cesium.defaultValue(e, Cesium.defaultValue.EMPTY_OBJECT),
        Measurement.call(this, e);
    var t = [new Cesium.Cartesian3, new Cesium.Cartesian3]
        , i = this._pointCollection;
    this._startPoint = i.add(MeasurementSettings.getPointOptions()),
        this._endPoint = i.add(MeasurementSettings.getPointOptions()),
        this._polyline = this._primitives.add(new PolylinePrimitive(MeasurementSettings.getPolylineOptions({
            ellipsoid: this._scene.frameState.mapProjection.ellipsoid,
            positions: t
        }))),
        this._label = this._labelCollection.add(MeasurementSettings.getLabelOptions({
            horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
            verticalOrigin: Cesium.VerticalOrigin.TOP,
            pixelOffset: new Cesium.Cartesian2(10,10)
        })),
        this._positions = t,
        this._distance = 0
}
HeightMeasurement.prototype = Object.create(Measurement.prototype),
    HeightMeasurement.prototype.constructor = HeightMeasurement,
    Cesium.defineProperties(HeightMeasurement.prototype, {
        distance: {
            get: function() {
                return this._distance
            }
        },
        icon: {
            value: getIcon(15)
        },
        thumbnail: {
            value: getIcon(25)
        },
        type: {
            value: "离地高度"
        },
        instructions: {
            value: ["单击点云以获取从该点到地形的距离"]
        },
        id: {
            value: "heightMeasurement"
        }
    }),
    HeightMeasurement.prototype.handleClick = function(e) {
        var t = this._scene;
        this.reset();
        var i = this._positions
            , r = HeightMeasurement._getWorldPosition(t, e, i[0]);
        if (Cesium.defined(r)) {
            var n = t.globe
                , a = t.frameState.mapProjection.ellipsoid
                , o = a.cartesianToCartographic(r, scratchCarto$3);
            Cesium.defined(n) ? o.height = Cesium.defaultValue(n.getHeight(o), 0) : o.height = 0;
            var s = a.cartographicToCartesian(o, i[1])
                , l = Cesium.Cartesian3.subtract(s, r, scratch)
                , c = Cesium.Cartesian3.magnitude(l)
                , u = this._label;
            u.position = r,
                u.show = !0,
                u.text = MeasureUnits.distanceToString(c, this._selectedUnits.distanceUnits, this._selectedLocale),
                this._polyline.positions = i,
                this._polyline.show = !0,
                this._startPoint.position = r,
                this._startPoint.show = !0,
                this._endPoint.position = s,
                this._endPoint.show = !0,
                this._distance = c
        }
    }
    ,
    HeightMeasurement.prototype.reset = function() {
        this._polyline.show = !1,
            this._label.show = !1,
            this._startPoint.show = !1,
            this._endPoint.show = !1,
            this._distance = 0
    }
    ,
    HeightMeasurement.prototype.isDestroyed = function() {
        return !1
    }
    ,
    HeightMeasurement.prototype.destroy = function() {
        this._primitives.remove(this._polyline);
        var e = this._pointCollection;
        return e.remove(this._startPoint),
            e.remove(this._endPoint),
            this._labelCollection.remove(this._label),
            Cesium.destroyObject(this)
    }
    ,
    HeightMeasurement._getWorldPosition = getWorldPosition;

export default  HeightMeasurement;